<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreCompanyRequest;
use App\Models\Company;
use App\Services\CompanyService;
use Illuminate\Http\Request;

class CompanyController extends Controller
{
    protected $CompanyService;
    public function __construct(CompanyService $CompanyService)
    {
        $this->CompanyService = $CompanyService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        
        $sql = Company::withCount('projects')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });
        
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Company::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新用户。
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  // title 企业名称
     * @param  // address 企业地址
     * @param  // logo 企业logo
     * @param  // uscc 统一社会信用代码
     * @param  // mobile 联系电话
     * @param  // contacts 联系人
     * @param  // scale 规模
     * @param  // thali 权限 体验：experience；会员：vip；高级会员:vvip
     * @param  // expire 过期日期
     * @param  // status 状态
     * @return \Illuminate\Http\Response
     */
    public function store(StoreCompanyRequest $Request)
    {
        //判断手机号码是否存在
        $is_mobile = Company::where('mobile',$Request->mobile)->first();
        if($is_mobile){
            return response()->json(['code' => '404', 'msg' => '该手机号码已绑定其他企业']);
        }

        $POSTONLY = Company::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->CompanyService->insert($input);
        //手机号作为管理员
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(StoreCompanyRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $is_mobile = Company::where('mobile',$Request->mobile)->where('id','<>',$Request->id)->first();
        if($is_mobile){
            return response()->json(['code' => '404', 'msg' => '该手机号码已绑定其他企业']);
        }
        $POSTONLY = Company::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->CompanyService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function status(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Company::find($Request->id);
        if(!$data){
            return response()->json(['code' => '404', 'msg' => '记录不存在']);
        }
        $data = $this->CompanyService->status($data);
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Company::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
